import os
import subprocess

# ui转py工具
def ui_to_py(ui_file):
    """Converts a Qt Designer .ui file to a Python .py file in the same directory."""
    # Get the base name without extension
    base_name = os.path.splitext(ui_file)[0]
    # Construct the output .py file name
    py_file = base_name + '.py'

    # Construct the command
    command = f'pyuic5 -o {py_file} {ui_file}'

    try:
        # Run the command
        subprocess.run(command, check=True, shell=True)
        print(f'Successfully converted {ui_file} to {py_file}')
    except subprocess.CalledProcessError as e:
        print(f'Failed to convert {ui_file} to {py_file}: {e}')


if __name__ == "__main__":
    # Get the current directory
    current_directory = os.getcwd()

    # Loop through all files in the current directory
    for file_name in os.listdir(current_directory):
        # Check if the file is a .ui file
        if file_name.endswith('.ui'):
            # Convert the .ui file to .py file
            ui_to_py(file_name)
